import { defineStore } from 'pinia';

// 定义五子棋游戏状态store
export const useGomokuStore = defineStore('gomoku', {
  state: () => ({
    // 游戏棋盘状态
    board: Array(15).fill().map(() => Array(15).fill(0)),
    
    // 游戏玩家状态
    players: [
      { id: 1, name: '玩家1', piece: 1, wins: 0 },
      { id: 2, name: '玩家2', piece: 2, wins: 0 }
    ],
    
    // 当前游戏状态
    currentPlayerId: 1,
    winner: null,
    gameOver: false,
    
    // 游戏设置
    settings: {
      showLastMove: true,
      showCoordinates: true
    },
    
    // 游戏历史
    moveHistory: [],
    
    // UI状态
    showSettings: false,
    showRules: false
  }),

  getters: {
    /**
     * 获取当前玩家
     * @returns {Object} 当前玩家对象
     */
    currentPlayer(state) {
      return state.players.find(player => player.id === state.currentPlayerId);
    },
    
    /**
     * 检查是否有玩家获胜
     * @returns {Boolean} 是否有玩家获胜
     */
    hasWinner(state) {
      return state.winner !== null;
    },
    
    /**
     * 获取获胜玩家
     * @returns {Object|null} 获胜玩家对象或null
     */
    getWinner(state) {
      if (!state.winner) return null;
      return state.players.find(player => player.id === state.winner);
    }
  },

  actions: {
    /**
     * 初始化游戏
     */
    initGame() {
      this.board = Array(15).fill().map(() => Array(15).fill(0));
      this.currentPlayerId = 1;
      this.winner = null;
      this.gameOver = false;
      this.moveHistory = [];
    },
    
    /**
     * 落子
     * @param {Number} row - 行索引
     * @param {Number} col - 列索引
     * @returns {Boolean} 是否落子成功
     */
    makeMove(row, col) {
      // 检查游戏是否已结束或位置是否已被占用
      if (this.gameOver || this.board[row][col] !== 0) {
        return false;
      }
      
      // 落子
      this.board[row][col] = this.currentPlayerId;
      this.moveHistory.push({ row, col, player: this.currentPlayerId });
      
      // 检查是否获胜
      if (this.checkWin(row, col)) {
        this.winner = this.currentPlayerId;
        this.gameOver = true;
        this.players = this.players.map(player => 
          player.id === this.currentPlayerId ? { ...player, wins: player.wins + 1 } : player
        );
        return true;
      }
      
      // 检查是否平局
      if (this.checkDraw()) {
        this.gameOver = true;
        return true;
      }
      
      // 切换玩家
      this.currentPlayerId = this.currentPlayerId === 1 ? 2 : 1;
      return true;
    },
    
    /**
     * 检查是否获胜
     * @param {Number} row - 行索引
     * @param {Number} col - 列索引
     * @returns {Boolean} 是否获胜
     */
    checkWin(row, col) {
      const player = this.board[row][col];
      if (!player) return false;
      
      // 检查四个方向：水平、垂直、两个对角线
      const directions = [
        [0, 1],  // 水平
        [1, 0],  // 垂直
        [1, 1],  // 对角线
        [1, -1]  // 反向对角线
      ];
      
      for (const [dx, dy] of directions) {
        let count = 1; // 包含当前棋子
        
        // 正向检查
        for (let i = 1; i < 5; i++) {
          const r = row + dx * i;
          const c = col + dy * i;
          if (r >= 0 && r < 15 && c >= 0 && c < 15 && this.board[r][c] === player) {
            count++;
          } else {
            break;
          }
        }
        
        // 反向检查
        for (let i = 1; i < 5; i++) {
          const r = row - dx * i;
          const c = col - dy * i;
          if (r >= 0 && r < 15 && c >= 0 && c < 15 && this.board[r][c] === player) {
            count++;
          } else {
            break;
          }
        }
        
        // 如果连成五子则获胜
        if (count >= 5) {
          return true;
        }
      }
      
      return false;
    },
    
    /**
     * 检查是否平局
     * @returns {Boolean} 是否平局
     */
    checkDraw() {
      // 检查棋盘是否已满
      for (let row = 0; row < 15; row++) {
        for (let col = 0; col < 15; col++) {
          if (this.board[row][col] === 0) {
            return false;
          }
        }
      }
      return true;
    },
    
    /**
     * 悔棋
     */
    undoMove() {
      if (this.moveHistory.length === 0 || this.gameOver) {
        return;
      }
      
      // 获取最后一步
      const lastMove = this.moveHistory.pop();
      
      // 撤销落子
      this.board[lastMove.row][lastMove.col] = 0;
      
      // 重置获胜状态
      this.winner = null;
      this.gameOver = false;
      
      // 切换回上一个玩家
      this.currentPlayerId = lastMove.player;
    },
    
    /**
     * 重置游戏
     */
    resetGame() {
      this.initGame();
    },
    
    /**
     * 切换设置面板显示状态
     */
    toggleSettings() {
      this.showSettings = !this.showSettings;
    },
    
    /**
     * 切换规则面板显示状态
     */
    toggleRules() {
      this.showRules = !this.showRules;
    }
  }
});